mysql一主多从同步配置

一、环境

        master:192.168.101
        MYSQL版本:5.1.48-community-log

        slave1:192.168.2.182
        MYSQL版本:5.1.48-community-log

        slave2:192.168.2.111
        MYSQL版本:5.1.48-community-log

        so…1 vs 2。

二、master和 slave上的相关配置

        3台上都一样:

        在/etc目录下可能无my.cnf文件,从/user/share/mysql目录中拷贝my-medium.cnf 到/etc并修改成my.cnf

1
2
3
4
[root@localhost etc]# cp /usr/share/mysql/my-medium.cnf my.cnf
[root@localhost etc]# ll |grep my
-rwxr-xr-x 1 root root 5204 Feb 13 22:52 my_bak
-rwxr-xr-x 1 root root 4765 Jul 10 23:07 my.cnf

        master 上;

1
[root@mysql101 ~]# vi /etc/my.cnf

1.修改master上的配置文件my.cnf。

        在[mysqld]下添加如下字段:

1
2
3
4
5
server-id = 1
log-bin=mysql-bin
binlog-do-db=YYY //需要同步的数据库
binlog-ignore-db=mysql //被忽略的数据库
binlog-ignore-db=information-schema //被忽略的数据库

        在master上为slave添加一个同步账号

1
2
3
4
mysql> grant replication slave on *.* to 'affairlog'@'192.168.2.182' identified by 'pwd123';
//在slave1上登陆成功
mysql> grant replication slave on *.* to 'affairlog'@'192.168.2.111' identified by 'pwd123';
//在slave2上登陆成功

        保存后,重启master的mysql服务:

1
service mysql restart;

        用show master status命令查看日志情况

1
2
3
4
5
6
7
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000087
Position: 106
Binlog_Do_DB: YYY
Binlog_Ignore_DB: mysql,information-schema
1 row in set (0.00 sec)

2.修改slave1上的配置文件my.cnf。

        在[mysqld]下添加如下字段

1
2
3
4
5
6
7
8
9
10
[root@mysql182 ~]# vi /etc/my.cnf
server-id=182
master-host=192.168.3.101
master-user= affairlog
master-password=pwd123
master-port=3306
master-connect-retry=60
replicate-do-db=YYY //同步的数据库
replicate-ignore-db=mysql //被忽略的数据库
replicate-ignore-db=information-schema //被忽略的数据库

        保存后,重启slave的mysql服务:

1
service mysql restart;

        修改slave2上的配置文件my.cnf,和上面类似,只是把server-id改下,为了方便,我都用了相应的ip某位,

        所以,slave2上我设置的server-id是111。

        在进入两个slave机中的mysql。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
mysql>start slave;
mysql>show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.3.101
Master_User: affairlog
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000087
Read_Master_Log_Pos: 106
Relay_Log_File: vm111-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000087
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: YYY
Replicate_Ignore_DB: mysql,information-schema
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 106
Relay_Log_Space: 406
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)

        如果两个slave中的Slave_IO_Running、Slave_SQL_Running状态均为Yes则表明设置成功。

三、可能遇到的问题:

问题1:Slave_IO_Running: No或者Slave_SQL_Running: No

停掉slave服务

1
2
mysql> slave stop;
Query OK, 0 rows affected (2.01 sec)

解决办法

  1. 在master上查看。
1
2
3
4
5
6
7
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000087
Position: 1845
Binlog_Do_DB: YYY
Binlog_Ignore_DB: mysql,information-schema
1 row in set (0.00 sec)
  1. 到slave上手动同步。
1
2
3
4
5
6
7
mysql>change master to
>master_host='192.168.3.101',
>master_user='affairlog',
>master_password='pwd123',
>master_log_file='mysql-bin.000087',
>master_log_pos=1845;
Query OK, 0 rows affected (0.00 sec)

启动slave服务

1
mysql> slave start;

再次查看Slave_IO_Running、Slave_SQL_Running状态,为Yes则表明设置成功。

问题2:RROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first

        错误重现:

1
2
3
4
5
6
7
mysql>change master to
>master_host='192.168.3.101',
>master_user='affairlog',
>master_password='pwd123',
>master_log_file='mysql-bin.000087',
>master_log_pos=1845;
ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first

        解决方法:

  1. 停掉slave服务
1
2
mysql> slave stop;
Query OK, 0 rows affected (2.01 sec)
  1. 重置slave服务
1
2
mysql> reset stop;
Query OK, 0 rows affected (2.01 sec)
  1. 再执行一次change命令
1
2
3
4
5
6
7
mysql>change master to
>master_host='192.168.3.101',
>master_user='affairlog',
>master_password='pwd123',
>master_log_file='mysql-bin.000087',
>master_log_pos=1845;
Query OK, 0 rows affected (0.00 sec)
  1. 启动slave服务
1
mysql> slave start;

5.再次查看Slave_IO_Running、Slave_SQL_Running状态,为Yes则表明设置成功。

PS:

        Slave_IO_Running:连接到主库,并读取主库的日志到本地,生成本地日志文件

        Slave_SQL_Running:读取本地日志文件,并执行日志里的SQL命令。